package com.ciban.smarthome.module.db.dao;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.ciban.smarthome.common.utils.StringUtils;
import com.ciban.smarthome.module.db.DbBaseDao;
import com.ciban.smarthome.module.entity.Host;
import com.ciban.smarthome.module.utils.ModuleUtils;

/**
 * 主机相关的数据库操作类
 */
public class HostDao extends DbBaseDao {

	/*CREATE TABLE host(_id integer PRIMARY KEY autoincrement, name varchar(30),serial_num integer,ip varchar(50),port varchar(10))*/

    public HostDao(Context context) {
        super(context, "host");
    }

    /**
     * 添加新的主机
     *
     * @param info
     * @return
     */
    public long addNewMain(Host info) {
        ContentValues values = new ContentValues();
        values.put("_id", ModuleUtils.getIDByRule());
        values.put("serial_num", info.getSerial_no());
        values.put("ip", info.getIp());
        values.put("port", info.getPort());
        values.put("name", info.getName());
        return insert(null, values);
    }

    /**
     * 获取所有的主机列表 若没有则返回为null
     *
     * @return
     */
    public ArrayList<Host> getMainEngList() {
        ArrayList<Host> list = new ArrayList<Host>();
        ;
        Cursor cursor = query(new String[]{"_id", "serial_num", "name",
                "ip", "port"}, null, null, null);
        if (cursor != null) {
            Host info;
            while (cursor.moveToNext()) {
                info = new Host();
                info.setId(cursor.getString(0));
                info.setSerial_no(cursor.getInt(1));
                info.setName(cursor.getString(2));
                info.setIp(cursor.getString(3));
                info.setPort(cursor.getInt(4));

                list.add(info);
            }
        }
        return list;
    }

    public Host getMainEngineDetail(String id) {
        if (StringUtils.isEmpty(id))
            return null;

        Cursor cursor = query(new String[]{"_id", "serial_num", "name",
                        "ip", "port"}, "_id=?",
                new String[]{id}, null);
        Host info = null;
        if (cursor.moveToNext()) {
            info = new Host();
            info.setId(cursor.getString(0));
            info.setSerial_no(cursor.getInt(1));
            info.setName(cursor.getString(2));
            info.setIp(cursor.getString(3));
            info.setPort(cursor.getInt(4));
        }
        return info;
    }

    public Host getMainEngineDetailbySerno(String serno) {
        if (StringUtils.isEmpty(serno))
            return null;

        Cursor cursor = query(new String[]{"_id", "serial_num", "name",
                        "ip", "port"}, "serial_num=?",
                new String[]{serno}, null);
        Host info = null;
        if (cursor.moveToNext()) {
            info = new Host();
            info.setId(cursor.getString(0));
            info.setSerial_no(cursor.getInt(1));
            info.setName(cursor.getString(2));
            info.setIp(cursor.getString(3));
            info.setPort(cursor.getInt(4));
        }
        return info;
    }

    /**
     * 通过主机id 删除主机
     *
     * @param id
     * @return
     */
    public int delMainEngine(String id) {
        int del = del("_id=?", new String[]{id});
        return del;
    }

    public int updateHost(Host info) {
        ContentValues values = new ContentValues();
        values.put("serial_num", info.getSerial_no());
        values.put("ip", info.getIp());
        values.put("port", info.getPort());
        values.put("name", info.getName());

        return update(values, "_id=?", new String[]{info.getId()});
    }

}
